﻿using AutoMapper;
using Harmontronics.DataAccess.IOperate.Business;
using Harmontronics.DataAccess.Operate.Business;
using Harmontronics.IService;
using Harmontronics.IService.Business;
using Harmontronics.Model.Dto;
using Harmontronics.Model.Dto.Model;
using Harmontronics.Model.Entitys;
using Harmontronics.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Harmontronics.Service
{
    public class PartService: IPartService
    {
        private IPartOperate _partOperate;
        private IRecipeOperate _recipeOperate;
        private readonly IMapper _mapper;
        private readonly IUnitOfWork _unitOfWork;
        public PartService(IUnitOfWork unitOfWork, IMapper mapper, IRecipeOperate recipeOperate, IPartOperate partOperate)
        {
            _mapper = mapper;
            _unitOfWork = unitOfWork;
            _recipeOperate = recipeOperate;
            _partOperate = partOperate;
        }

        public List<PartModel> MendingRange(List<Part> entities)
        {
            List<PartModel> models = new List<PartModel>();
            foreach (var entity in entities)
            {
                models.Add(Mending(entity));
            }
            return models;
        }

        public PartModel Mending(Part entity)
        {
            PartModel model = _mapper.Map<PartModel>(entity);
            Recipe objEntity = _recipeOperate.GetRecipeById(entity.RecipeId);
            if(objEntity != null)
            {
                model.Recipe = objEntity;
            }
            else
            {
                model.Recipe = new Recipe()
                {
                    RecipeId = entity.RecipeId,
                };
            }
            return model;
        }
        public List<Part> GetAllParts()
        {
            try
            {
                return _partOperate.GetAllPartList();
            }
            catch (Exception ex)
            {
                return new List<Part>();
            }
        }

        public Part GetPartById(string id)
        {
            try
            {
                return _partOperate.GetPartById(id);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public bool addPart(Part entity)
        {
            try
            {
                _partOperate.AddPart(entity);
                _unitOfWork.Commit();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool updatePart(Part model)
        {
            try
            {
                Part entity = _partOperate.GetPartById(model.PartId);
                _mapper.Map(model, entity);
                _partOperate.UpdatePart(entity);
                _unitOfWork.Commit();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool DeletePart(string id)
        {
            try
            {
                Part entity = _partOperate.GetPartById(id);
                if (entity != null)
                {
                    _partOperate.DeletePart(entity);
                    _unitOfWork.Commit();
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool BatchDelPart(List<string> ids)
        {
            try
            {
                List<Part> entities = _partOperate.GetPartByIdList(ids);
                if (entities.Count > 0)
                {
                    _partOperate.BatchDelPart(entities);
                    _unitOfWork.Commit();
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }


        }
    }
}
